package com.ihr360.cnb.module.oneclick.controller;

import com.ihr360.cnb.module.oneclick.service.IrenshiDubboProviderService;
import com.ihr360.cnb.module.oneclick.service.OneClickService;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.oneclick.entity.StaffRecord;
import com.ihr360.oneclick.vo.MessageGroupVo;
import com.ihr360.oneclick.vo.StaffMonthMessageVo;
import com.ihr360.oneclick.vo.WechatStaffInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 一键工资条 微信接口
 *
 * @author tom
 * @date 18-1-12
 */

@RestController
@RequestMapping("/oneclick")
@Api(value = "/oneclick", description = "微信相关接口", produces = ("application/json;charset=UTF-8"))
//@DisableResponseAdvice
public class WeChatApiController {

    private static final Logger logger = LoggerFactory.getLogger(WeChatApiController.class);

    @Autowired
    private OneClickService oneClickService;

    @Autowired
    private IrenshiDubboProviderService irenshiDubboProviderService;

    @GetMapping("/function/staff/month/list")
    @ApiOperation(value = "/function/staff/month/list", httpMethod = "GET", notes = "获取用户对于发布数据的所属月份列表",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo<List<MessageGroupVo>> getFunctionDataMonthList(@RequestParam String functionId, AuthUserInfo authUserInfo) {
        // 获取oneClick 中的所有月份
        List<String> monthList = oneClickService.getFunctionDataMonthList(functionId, authUserInfo);

        /*// 如果员工查询的是工资条 获取i人事薪资模块
        if (ConstantString.ONE_CLICK_PAYROLL_ID.equals(functionId) && authUserInfo.getStaffId() != null) {
            logger.info("get irenshi payroll start, mobile: {}.", authUserInfo.getMobileNo());
            List<String> salaryMonthList = irenshiDubboProviderService.getSalaryMonthList(authUserInfo);
            logger.info("get irenshi payroll end, mobile: {}.", authUserInfo.getMobileNo());
            if(salaryMonthList == null) {
                logger.info("invoke irenshi-dubbo-provider getSalaryMonthList failed, mobile: {}.", authUserInfo.getMobileNo());
            }
            if (!CollectionUtils.isEmpty(salaryMonthList)) {
                salaryMonthList.forEach(monthStr -> {
                    if(!monthList.contains(monthStr)) {
                        monthList.add(monthStr);
                    }
                });
            }
        }*/

        // 排序
        Collections.sort(monthList, (o1, o2) -> o2.compareTo(o1));
        List<MessageGroupVo> messageGroupVoList = new ArrayList<>();
        monthList.forEach(monthly -> {
            String[] months = monthly.split("-");
            MessageGroupVo messageGroupVo = new MessageGroupVo();
            messageGroupVo.setYear(months[0]+"年");
            messageGroupVo.setMonth(months[1]+"月");
            messageGroupVo.setGroupId(monthly);
            messageGroupVoList.add(messageGroupVo);
        });
        return new ResultInfo.Builder<List<MessageGroupVo>>().success(messageGroupVoList);
    }

    @GetMapping("/function/staff/data")
    @ApiOperation(value = "/function/staff/data", httpMethod = "GET", notes = "获取用户对应发布数据",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo<StaffMonthMessageVo> getFunctionStaffDataByMonth(@RequestParam String functionId,
                                                                       @RequestParam String groupId,
                                                                       AuthUserInfo authUserInfo) {
        // 获取oneClick中的消息
        StaffMonthMessageVo staffMonthMessageVo = oneClickService.getFunctionStaffDataByMonth(functionId, groupId, authUserInfo);
        if(staffMonthMessageVo == null){
            staffMonthMessageVo = new StaffMonthMessageVo();
            staffMonthMessageVo.setMonth(groupId);
            staffMonthMessageVo.setBatchMessageList(new ArrayList<>());
        }
       /* // 如果查询的是工资条获取 i人事薪资模块的工资条
        if (ConstantString.ONE_CLICK_PAYROLL_ID.equals(functionId) && authUserInfo.getStaffId() != null) {
            StaffMonthMessageVo staffSalaryDetailVo = irenshiDubboProviderService.getSalaryDetail(authUserInfo, groupId);
            // 合并
            if(staffSalaryDetailVo != null && !CollectionUtils.isEmpty(staffSalaryDetailVo.getBatchMessageList())) {
                staffMonthMessageVo.getBatchMessageList().addAll(staffSalaryDetailVo.getBatchMessageList());
            }
        }*/
        return new ResultInfo.Builder<StaffMonthMessageVo>().success(staffMonthMessageVo);
    }

    @PostMapping("/function/staff/checkName")
    @ApiOperation(value = "/function/staff/checkName", httpMethod = "GET", notes = "校验姓名",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public boolean checkLastName(@RequestParam String functionId,@RequestBody AuthUserInfo authUserInfo){
        oneClickService.checkLastName(functionId, authUserInfo);
        return true;
    }

    @GetMapping("/staff/info")
    @ApiOperation(value = "/staff/info", httpMethod = "GET", notes = "获取员工信息 微信端查看一键发布信息用于展示员工信息",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo<WechatStaffInfoVo> getStaffinfo(AuthUserInfo authUserInfo) {

        // TODO 如果在员工名录中没有查询到，就查询i人事花名册，逻辑待确认
        StaffRecord staffRecord = oneClickService.getStaffRecord(authUserInfo);

        WechatStaffInfoVo wechatStaffInfoVo = new WechatStaffInfoVo();
        if (staffRecord != null) {
            wechatStaffInfoVo.setMobileNo(staffRecord.getMobileNo());
            wechatStaffInfoVo.setStaffName(staffRecord.getStaffName());
        }
        return new ResultInfo.Builder<WechatStaffInfoVo>().success(wechatStaffInfoVo);
    }
}
